/**
 * 
 */
package de.idgard.android.addon.backend.impl;

import de.idgard.android.R;
import de.idgard.android.addon.backend.IDGBackendService;
import de.idgard.android.addon.services.GlobalServiceRegistry;
import de.idgard.android.addon.services.transport.JsonAndroidTransport;
import de.idgard.android.addon.ui.services.PropertiesService;
import de.idgard.json.data.PrivacyCounter;
import de.idgard.json.data.ProfileId;
import de.idgard.json.request.PrivacyCounterRequest;
import de.idgard.json.request.ResetPrivacyCounterRequest;
import de.idgard.json.request.SetTransparencyRequest;
import de.idgard.json.response.HistoryListResponse;
import de.idgard.json.response.PrivacyProfilesResponse;
import de.idgard.json.response.TransparencyChangeResponse;
import de.idgard.json.transport.AsyncRequest;
import de.idgard.json.transport.AsyncRequestImpl;
import de.idgard.json.transport.ServiceTransport;
import de.idgard.webapp.shared.dto.bookmark.BookmarkFolderDto;
import de.idgard.webapp.shared.dto.bookmark.BookmarkLinkDto;
import de.idgard.webapp.shared.dto.privacy.HistoryItemDto;
import de.idgard.webapp.shared.dto.profile.SimpleProfileDto;
import de.idgard.webapp.shared.dto.transport.DataRequest;

/**
 * 
 * @author <a href="mailto:vesko.georgiev@uniscon.com">Vesko Georgiev</a>
 */
class IDGBackendServiceImpl implements IDGBackendService {

	private BackendCacheManager backendCacheManager = new BackendCacheManager();

	private ServiceTransport jsonTransport;

	private PropertiesService propService;

	private String getURL(int ctxResId) {
		return propService.getString(ctxResId);
	}

	private String stripDomain(String domain) {
		String[] parts = domain.split("\\.");
		if (parts.length > 1) {
			return parts[parts.length - 2] + "." + parts[parts.length - 1];
		}
		return domain;
	}

	public IDGBackendServiceImpl() {
		propService = GlobalServiceRegistry.instance().getPropertiesService();
		jsonTransport = new JsonAndroidTransport(
				propService.getString(R.string.system_domain));
	}

	@Override
	public AsyncRequest<Void> saveBookmark(BookmarkLinkDto bookmark) {
		backendCacheManager.invalidateBookmarks();
		return new AsyncRequestImpl<Void>(Void.class, jsonTransport,
				getURL(R.string.service_add_bookmark_add), bookmark);
	}

	@Override
	public AsyncRequest<Void> savePrivacyData(SimpleProfileDto privacyProfile) {
		String url = getURL(R.string.service_add_privacy_data);

		return new AsyncRequestImpl<Void>(Void.class, jsonTransport, url,
				privacyProfile);
	}

	@Override
	public AsyncRequest<Void> logoff() {
		return new AsyncRequestImpl<Void>(Void.class, jsonTransport,
				getURL(R.string.service_logout_service), null);
	}

	@Override
	public AsyncRequest<PrivacyProfilesResponse> getPrivacyConnectProfiles(
			String host) {
		DataRequest<String> payload = new DataRequest<String>(
				stripDomain(host), "detail");
		String url = getURL(R.string.service_get_privacy_connect_host_profiles);

		return new AsyncRequestImpl<PrivacyProfilesResponse>(
				PrivacyProfilesResponse.class, jsonTransport, url, payload);
	}

	@Override
	public AsyncRequest<String> getPrivacyConnectProfileToken(final String id) {
		String url = getURL(R.string.service_get_profile_token);

		return new AsyncRequestImpl<String>(String.class, jsonTransport, url,
				new ProfileId(id));
	}

	@Override
	public AsyncRequest<PrivacyCounter> queryPrivacyCounter() {
		PrivacyCounterRequest payload = new PrivacyCounterRequest();
		String url = getURL(R.string.service_get_init_data);

		return new AsyncRequestImpl<PrivacyCounter>(PrivacyCounter.class,
				jsonTransport, url, payload);
	}

	@Override
	public AsyncRequest<Void> resetPrivacyCounter() {
		ResetPrivacyCounterRequest payload = new ResetPrivacyCounterRequest();
		String url = getURL(R.string.service_reset_data);

		return new AsyncRequestImpl<Void>(Void.class, jsonTransport, url,
				payload);
	}

	@Override
	public AsyncRequest<TransparencyChangeResponse> changeTransparencyLevel(
			int value) {
		return new AsyncRequestImpl<TransparencyChangeResponse>(
				TransparencyChangeResponse.class, jsonTransport,
				getURL(R.string.service_transparency_set_data),
				new SetTransparencyRequest(value + ""));
	}

	@Override
	public AsyncRequest<Void> submitHistoryEntry(HistoryItemDto entry) {
		String url = getURL(R.string.service_submit_history_entry);
		entry.setUserAgent("");
		entry.setAddonVersion("");
		entry.setReferer("");
		entry.setId("");
		entry.setDate("");
		DataRequest<HistoryItemDto> dr = new DataRequest<HistoryItemDto>(entry,
				DataRequest.LIST_OPTION_DETAIL);
		return new AsyncRequestImpl<Void>(Void.class, jsonTransport, url, dr);
	}

	@Override
	public AsyncRequest<BookmarkFolderDto> getBookmarks() {
		DataRequest<Void> req = new DataRequest<Void>(
				DataRequest.LIST_OPTION_PORTAL);
		AsyncRequestImpl<BookmarkFolderDto> areq = new AsyncRequestImpl<BookmarkFolderDto>(
				BookmarkFolderDto.class, jsonTransport,
				getURL(R.string.service_bookmark_list), req);
		areq.setCachedResult(backendCacheManager.getBookMarks());
		areq.setCacheHandler(backendCacheManager);
		return areq;
	}

	@Override
	public AsyncRequest<HistoryListResponse> getHistory(int count) {
		return new AsyncRequestImpl<HistoryListResponse>(
				HistoryListResponse.class, jsonTransport,
				getURL(R.string.service_history_list) + "?s=" + count);
	}
}
